package com.star.web.order.controller;

import com.star.entity.Result;
import com.star.order.feign.OrderFeign;
import com.star.order.pojo.Order;
import com.star.pay.feign.PayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \_________
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \        \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \        \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \        \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\        \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|         \
	 	  \___________________________________________________________________________________\

*/

@Controller
@RequestMapping("/wxpay")
public class PayController {

    @Autowired
    private PayFeign payFeign;

    @Autowired
    private OrderFeign orderFeign;
    /**
     * 微信跳转二维码页面
     * @param orderId 订单id
     * @return
     */
    @GetMapping
    public String WXPay(String orderId, Model model) {
        //1.根据id查询订单
        Order order = orderFeign.findById(orderId).getData();
        if (order == null) {
            //订单不存在
            return "fail";
        }
        //2.根据订单状态判断，如果不是未支付的订单，调到错误页面
        if (!order.getPayStatus().equals("0")) {
            //不是未支付订单
            return "fail";
        }
        //3.基于PayFeign接口调用 统计下单接口  ，获取返回数据
        Result payResult = payFeign.nativePay(orderId, order.getPayMoney());
        if (payResult.getData() == null) {
            return "fail";
        }
        Map payMap= (Map) payResult.getData();

        payMap.put("orderId", order);
        payMap.put("payMoney", order.getPayMoney());
        //4.封装数据结果
        model.addAllAttributes(payMap);

        return "wxpay";
    }

    //支付成功页面的跳转
    @RequestMapping("/toPaySuccess")
    public String toPaySuccess(Integer payMoney, Model model) {
        model.addAttribute("payMoney", payMoney);
        return "paysuccess";
    }
}
